package com.yksj.healthtalk.comm;

import org.handmark.pulltorefresh.library.PullToRefreshListView;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;

import com.yksj.healthtalk.adapter.CheckboxListAdapter;
import com.yksj.healthtalk.ui.R;
import com.yksj.healthtalk.ui.salon.SalonSpecialPeoplsMenberAddActivity;
import com.yksj.healthtalk.utils.SystemUtils;
import com.yksj.healthtalk.utils.ViewUtils;

public class CheckBoxListFragment extends Fragment implements OnClickListener, OnEditorActionListener, OnItemClickListener{
	
	public EditText mEditText;
	public PullToRefreshListView mListView;
	public CheckboxListAdapter mAdapter;
	public String mPatient_Group_ID;
	onSeachPerson seachPerson;
	public interface onSeachPerson{
		void seach(String str);
	};
	public void setOnSeachPerson(onSeachPerson seachPerson){
		this.seachPerson=seachPerson;
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		final View view = inflater.inflate(R.layout.check_box_list_layout,null);
		mEditText  = (EditText)view.findViewById(R.id.seach_text);
		final View cancle = view.findViewById(R.id.seach_onclick);
		mEditText.setOnFocusChangeListener(new OnFocusChangeListener() {
			
			@Override
			public void onFocusChange(View v, boolean hasFocus) {
				
				ViewUtils.setGone(cancle, !hasFocus);
			}
		});
		view.findViewById(R.id.deleteText).setOnClickListener(this);
		cancle.setOnClickListener(this);
		mListView = (PullToRefreshListView)view.findViewById(R.id.pull_refresh_listview);
		mListView.setOnItemClickListener(this);
		mEditText.setOnEditorActionListener(this);
		return view;
	}
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		if(mAdapter == null){
			mAdapter = new CheckboxListAdapter(getLayoutInflater(savedInstanceState),getFragmentManager());
		}
		mListView.setAdapter(mAdapter);
		if(getActivity() instanceof SalonSpecialPeoplsMenberAddActivity){
			SalonSpecialPeoplsMenberAddActivity activity = ((SalonSpecialPeoplsMenberAddActivity)getActivity());
			if(activity.Special_Group_ID != null){
				mAdapter.mPatient_Group_ID = activity.Special_Group_ID;
			}else{
				mAdapter.mPatient_Group_ID = activity.Service_Group_ID;
			}
		}
	}
	
	@Override
	public void onClick(View v) {
		switch(v.getId()){
		case R.id.deleteText:
			mEditText.setText(null);
			break;
		case R.id.seach_onclick://查询
			SystemUtils.hideSoftBord(getActivity(), mEditText);
			v.setVisibility(View.GONE);
			break;
		}
	}

	@Override
	public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
		String str = mEditText.getText().toString();
		if (str.trim().length() == 0)return false;
		v.setTag("seach"); ((OnClickListener) getActivity()).onClick(v);
		return true;
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		mAdapter.onCheck(position-1);
	}
	
}
